package com.example.storedownload;

import androidx.annotation.IdRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.ContextThemeWrapper;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class storedownload extends AppCompatActivity {

    AlertDialog.Builder builder;
    ProgressBar progressBar;
    private ExecutorService ThreadPool;
    private Handler handler;

    private int progressStatus = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_storedownload);

        builder = new AlertDialog.Builder(storedownload.this);
        builder.setTitle(getResources().getString(R.string.dialog_title));
        builder.setView(R.layout.dialog_progress_layout);
        builder.setPositiveButton(getResources().getString(R.string.dialog_btn_cancel),null);
        AlertDialog dialog = builder.create();
        dialog.show();


        progressBar = (ProgressBar)dialog.findViewById(R.id.dialog_progress);
        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == getIntResource(R.integer.status_running))
                {
                    progressBar.setProgress(progressStatus);
                }
                if (msg.what == getIntResource(R.integer.status_done))
                {
                    dialog.setTitle(getResources().getString(R.string.dialog_title_done));
                    if (dialog.isShowing())
                    {
                        dialog.cancel();
                    }
                    Toast.makeText(storedownload.this,getResources().getString(R.string.dialog_tip_done),Toast.LENGTH_LONG).show();
                }

            }
        };

        ThreadPool = Executors.newSingleThreadExecutor();
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                while (true)
                {
                    progressStatus = progressWork();
                    Message m = new Message();
                    if (progressStatus < getResources().getInteger(R.integer.progress_max))
                    {
                        m.what = getIntResource(R.integer.status_running);
                        handler.sendMessage(m);
                    }
                    else
                    {
                        m.what = getIntResource(R.integer.status_done);
                        handler.sendMessage(m);
                        break;
                    }
                }
            }
            private int progressWork(){
                progressStatus += Math.random() * 5;
                try {
                    Thread.sleep(100);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                return progressStatus;
            }
        });

    }

    public int getIntResource(@IntegerRes int id)
    {
        return getResources().getInteger(id);
    }
}